package com.example.lib_base.base.network;

import android.text.TextUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;

/**
 * Created by zdj on 16-5-29.
 */

public class BaseRequest {

    protected HashMap<String, String> params = new HashMap<>();

    public HashMap<String, String> getParams() {

        Field[] fields = getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.getModifiers() != Modifier.PRIVATE) {
                continue;
            }
            try {
                String key = field.getName();
                if (field.isAnnotationPresent(Key.class)) {
                    Key annotationKey = field.getAnnotation(Key.class);
                    String annotationValue = annotationKey.value();
                    if (!TextUtils.isEmpty(annotationValue)) {
                        key = annotationValue;
                    }
                }
                field.setAccessible(true);
                params.put(key, field.get(this) == null ? "" : String.valueOf(field.get(this)));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return params;
    }
}
